package com.das.mechanic_base.utils;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3AppRouterConstants;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.base.X3IBaseView;
import com.das.mechanic_base.bean.plan.PlanListBean;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.app.X3RouterCenter;
import com.das.mechanic_base.widget.X3NewBottomMileOrTimeDialog;
import com.das.mechanic_base.widget.X3PointDialog;
import com.umeng.analytics.MobclickAgent;

public class X3BottomChangePlanDialog extends X3BaseBottomSheetDialog implements View.OnClickListener, X3BottomDeletePlanDialog.IOnDeletePlan {

    private RelativeLayout rl_check;
    private RelativeLayout rl_run;
    private RelativeLayout rl_edit;
    private RelativeLayout rl_delete;
    private PlanListBean listBean;
    private View v_line;
    private X3PointDialog pointDialog;
    private X3NewBottomMileOrTimeDialog mileOrTimeDialog;
    private X3BottomDeletePlanDialog deletePlanDialog;

    public X3BottomChangePlanDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        rl_check = getView(R.id.rl_check);
        rl_run = getView(R.id.rl_run);
        rl_edit = getView(R.id.rl_edit);
        rl_delete = getView(R.id.rl_delete);
        v_line = getView(R.id.v_line);

        rl_check.setOnClickListener(this);
        rl_run.setOnClickListener(this);
        rl_edit.setOnClickListener(this);
        rl_delete.setOnClickListener(this);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_bottom_change_plan_dialog;
    }

    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("保养计划编辑与履行弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("保养计划编辑与履行弹窗");
    }

    public void showChangePlan(PlanListBean listBean) {
        this.listBean = listBean;
        if (listBean == null) {
            return;
        }
        rl_check.setVisibility(listBean.workBaseId == 0 ? View.GONE : View.VISIBLE);
        v_line.setVisibility(listBean.workBaseId == 0 ? View.GONE : View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.rl_check) {
            Bundle bundle = new Bundle();
            bundle.putString("path", X3Constent.HOME_FINISH_DETAIL_MAIN_URL + "workBaseId=" + listBean.workBaseId + "&carNum=" + listBean.carNum);
            bundle.putBoolean("isDark",true);
            bundle.putString("title","查看检测报告页");
            X3RouterCenter.startNavigate(X3AppRouterConstants.Router.GROUND_PUSH, bundle);


//            Intent intent = new Intent(mContext, GroundPushActivity.class);
//            intent.putExtra("isDark", true);
//            intent.putExtra("title", "查看检测报告页");
//            intent.putExtra("path", Constent.HOME_FINISH_DETAIL_MAIN_URL + "workBaseId=" + listBean.workBaseId + "&carNum=" + listBean.carNum);
//            mContext.startActivity(intent);
            dismiss();
        } else if (id == R.id.rl_run) {
            pointDialog = new X3PointDialog(mContext, mContext.getString(R.string.plan_run_title), mContext.getString(R.string.x3_no_tips),
                    mContext.getString(R.string.x3_yes_tips));
            pointDialog.show();
            pointDialog.setRightClick(v12 -> {
                pointDialog.dismiss();
                finishPlan();
            });
        } else if (id == R.id.rl_edit) {
            mileOrTimeDialog = new X3NewBottomMileOrTimeDialog(mContext, 1);
            if (mileOrTimeDialog.isShowing()) {
                mileOrTimeDialog.dismiss();
            } else {
                mileOrTimeDialog.show();
                mileOrTimeDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            }
            mileOrTimeDialog.modifyCurrentMiles(listBean.currentMiles, listBean.perMiles, listBean.nextReplaceCycleKm, listBean.reductionType, X3StringUtils.isEmpty(listBean.warningDate) ? listBean.date : listBean.warningDate);
            mileOrTimeDialog.setiOnAffirmMile((time, nextMiles, type) -> {
                listBean.id = listBean.carMaintencenPlanId;
                listBean.warningDate = time;
                listBean.nextReplaceCycleKm = Double.parseDouble(nextMiles);
                listBean.reductionType = (type == 0) ? "MILE" : "TIME";
                updatePlan();
            });
        } else if (id == R.id.rl_delete) {
            deletePlan();
        }

    }

    private void updatePlan() {
        NetWorkHttp.getApi().updateCarMaintenancePlan(listBean)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(((X3IBaseView)mContext).bindToLife())
                .subscribe(new HttpCallBack<Object>() {
                    @Override
                    protected void onDone(Object o) {
                        dismiss();
                        if (iOnRefreshPlan != null) {
                            iOnRefreshPlan.iOnRefreshPlan();
                        }
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    private void deletePlan() {
        if (deletePlanDialog == null) {
            deletePlanDialog = new X3BottomDeletePlanDialog(mContext);
        }
        deletePlanDialog.show();
        deletePlanDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        deletePlanDialog.showServiceName(listBean.serviceBaseName);
        deletePlanDialog.setiOnDeletePlan(this);

    }

    private void finishPlan() {
        NetWorkHttp.getApi().finishCarMaintenancePlan(listBean.carMaintencenPlanId)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(((X3IBaseView)mContext).bindToLife())
                .subscribe(new HttpCallBack<Object>() {
                    @Override
                    protected void onDone(Object o) {
                        dismiss();
                        if (iOnRefreshPlan != null) {
                            iOnRefreshPlan.iOnRefreshPlan();
                        }
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });

    }

    IOnRefreshPlan iOnRefreshPlan;

    public X3BottomChangePlanDialog setiOnRefreshPlan(IOnRefreshPlan iOnRefreshPlan) {
        this.iOnRefreshPlan = iOnRefreshPlan;
        return this;
    }

    @Override
    public void iOnDeletePlanAfter(int cause) {
        if (cause == 0) {
            // 履行保养计划
            finishPlan();
            X3ToastUtils.showMessage(mContext.getString(R.string.delete_success));
        }else {
            // 删除保养计划
            NetWorkHttp.getApi().deleteCarMaintenancePlan(listBean.carMaintencenPlanId)
                    .compose(RxSchedulersHelper.defaultComposeRequest())
                    .compose(((X3IBaseView)mContext).bindToLife())
                    .subscribe(new HttpCallBack<Object>() {
                        @Override
                        protected void onDone(Object o) {
                            dismiss();
                            X3ToastUtils.showMessage(mContext.getString(R.string.delete_success));
                            if (iOnRefreshPlan != null) {
                                iOnRefreshPlan.iOnRefreshPlan();
                            }
                        }

                        @Override
                        protected String LoadingMessage() {
                            return null;
                        }

                        @Override
                        protected void showError(String error) {

                        }
                    });
        }
    }

    public interface IOnRefreshPlan{
        void iOnRefreshPlan();
    }
}
